//
//  YMBaseTabBarController.m
//  YMMedical
//
//  Created by y s on 2023/6/19.
//

#import "YMBaseTabBarController.h"
#import "YMHomeViewController.h"
#import "YMMineViewController.h"
#import "YMCheckViewController.h"

#import "YMNavigationController.h"
#import "YMCustomTabBar.h"


@interface YMBaseTabBarController ()<UITabBarControllerDelegate,UITabBarDelegate>

@end

@implementation YMBaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
#pragma mark -- UI
- (void)setUI{
    self.delegate = self;
    
    // 1.添加所有的子控制器
    [self addAllChildViewControllers];
    [self setValue:[[YMCustomTabBar alloc] init] forKey:@"tabBar"];
}

- (void)addAllChildViewControllers{
    [self addOneViewController:[[YMHomeViewController alloc] init] image:@"tabbar_home_nomal" selectedImage:@"tabbar_home_selected" title:KLanguage(@"home_txt")];
    [self addOneViewController:[[YMCheckViewController alloc] init] image:@"tabbar_detection_icon" selectedImage:@"tabbar_detection_icon" title:KLanguage(@"start_dete_txt")];
    [self addOneViewController:[[YMMineViewController alloc] init] image:@"tabbar_mine_nomal" selectedImage:@"tabbar_mine_selected" title:KLanguage(@"mine_txt")];
    
}

- (void)addOneViewController:(UIViewController *)childViewController
                       image:(NSString *)imageName
               selectedImage:(NSString *)selectedImageName
                       title:(NSString *)title{

    YMNavigationController *nav = [[YMNavigationController alloc] initWithRootViewController:childViewController];
    //设置图片和文字之间的间
    if ([title isEqualToString:KLanguage(@"start_dete_txt")]) {
        nav.tabBarItem.imageInsets = UIEdgeInsetsMake(-8, 0, 8, 0);
    }
    nav.tabBarItem.title = title;
    if (imageName.length) { // 图片名有具体
        nav.tabBarItem.image = [self imageWithOriRenderingImage:imageName];
        nav.tabBarItem.selectedImage = [self imageWithOriRenderingImage:selectedImageName];
    }
    [self addChildViewController:nav];
}

- (UIImage *)imageWithOriRenderingImage:(NSString *)imageName{
    UIImage *image = [UIImage imageNamed:imageName];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if ([self.tabBar.selectedItem.title isEqualToString:KLanguage(@"start_dete_txt")]){
        return NO;
    }
    return YES;
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    if ([item.title isEqualToString:KLanguage(@"start_dete_txt")]) {
        NSString *deviceSn = [YMMyModel shared].deviceId;
        if (deviceSn == nil || deviceSn.length < 1){
            [SHUD showInfo:KLanguage(@"choose_home_device_txt")];
            return;
        }
        YMCheckViewController *vc = [[YMCheckViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [SJumpUtil pushVC:vc animated:YES];
    }
}


@end
